<?php

namespace app\api\controller;

use app\common\RedisCache;
use think\facade\Validate;
use think\facade\Db;
use app\lib\Result;

class Captcha extends Base
{
  /**
   * 列表
   * @Author Qianlong <87498106@qq.com>
   * @PersonSite http://www.21ds.cn/
   */
  public function list()
  {
    $rule = [
      'pageNo'  => 'number',
      'pageSize'  => 'number',
    ];
    $data = [
      'pageNo' => input('param.pageNo', 1, 'trim'),
      'title' => input('param.title', '', 'trim'),
      'pageSize' => input('param.pageSize', 15, 'trim'),
    ];
    $validate = Validate::rule($rule);
    $result   = $validate->check($data);
    if (!$result) {
      return Result::Error($validate->getError());
    }
    // 构建查询条件
    $where = [];

    if (!empty($data['title'])) {
      $where[] = ['title|_id', 'like', $data['title']];
    }
    $limit = ($data['pageNo'] - 1) * $data['pageSize'];
    $dataList = Db::connect('mongodb')->name('captcha_list')->field('*')->where($where)->order('create_time', 'desc')->limit($limit, $data['pageSize'])->select()->toArray();
    $totalcount = Db::connect('mongodb')->name('captcha_list')->where($where)->count('id');
    foreach ($dataList as &$vo) {
      $vo['create_time'] = date('Y-m-d H:i:s', $vo['create_time']);
      switch ($vo['platform']) {
        case 'jdcloud':
          $vo['platform_title'] = '京东云验证码';
          break;
        case 'aliyun':
          $vo['platform_title'] = '阿里云验证码';
          break;
        case 'tencent':
          $vo['platform_title'] = '腾讯云验证码';
          break;
        case 'geetest':
          $vo['platform_title'] = '极验';
          break;
        default:
          $vo['platform_title'] = '未知';
          break;
      }
    }
    $returnData['data']['data'] = $dataList;
    $returnData['data']['totalCount'] = $totalcount;
    $returnData['data']['pageNo'] = (int) $data['pageNo'];
    $returnData['data']['pageSize'] = (int) $data['pageSize'];
    return Result::Success($returnData['data']);
  }
  /**
   * 编辑文章
   * @Author Qianlong <87498106@qq.com>
   * @PersonSite http://www.21ds.cn/
   */
  public function editItemInfo()
  {
    if (request()->isPost()) {
      $rule = [
        'title|标题'  => 'require|max:32',
        'platform|验证平台'  => 'require',
        'status|状态'  => 'require',
        'server_secret|SecretKey'  => 'require',
      ];
      $data = [
        '_id' => input('param._id', '', 'trim'),
        'title' => input('param.title', '', 'trim'),
        'platform' => input('param.platform', '', 'trim'),
        'jdcloud_app_id' => input('param.jdcloud_app_id', '', 'trim'),
        'jdcloud_scene_id' => input('param.jdcloud_scene_id', '', 'trim'),
        'jdcloud_scene_secret' => input('param.jdcloud_scene_secret', '', 'trim'),
        'jdcloud_access_key_id' => input('param.jdcloud_access_key_id', '', 'trim'),
        'jdcloud_access_key_secret' => input('param.jdcloud_access_key_secret', '', 'trim'),
        'jdcloud_client_type' => input('param.jdcloud_client_type', '', 'trim'),
        'geetest_key' => input('param.geetest_key', '', 'trim'),
        'geetest_id' => input('param.geetest_id', '', 'trim'),
        'geetest_style_width' => input('param.geetest_style_width', '', 'trim'),
        'geetest_style_height' => input('param.geetest_style_height', '', 'trim'),
        'geetest_type' => input('param.geetest_type', '', 'trim'),
        'aliyun_captcha_key' => input('param.aliyun_captcha_key', '', 'trim'),
        'aliyun_key_secret' => input('param.aliyun_key_secret', '', 'trim'),
        'aliyun_key_id' => input('param.aliyun_key_id', '', 'trim'),
        'status' => input('param.status', '', 'trim'),
        'server_secret' => input('param.server_secret', '', 'trim'),
        'tenc_secret_id' => input('param.tenc_secret_id', '', 'trim'),
        'tenc_secret_key' => input('param.tenc_secret_key', '', 'trim'),
        'tenc_app_id' => input('param.tenc_app_id', '', 'trim'),
        'aliyun_style_width' => input('param.aliyun_style_width', '', 'trim'),
        'tenc_app_secret' => input('param.tenc_app_secret', '', 'trim'),
        'ak_from' => input('param.ak_from', '', 'trim'),
        'ak_id' => input('param.ak_id', '', 'trim'),
      ];
      $validate = Validate::rule($rule);
      $result   = $validate->check($data);
      if (!$result) {
        return Result::Error($validate->getError());
      }
      $data['status'] = intval($data['status']);
      $captchaCount = Db::connect('mongodb')->name('captcha_list')->count();
      $this->security::checkVersionLimit($this->user['version'],'captcha_num','验证码数量', $captchaCount);
      if (!empty($data['_id'])) {
        $_id = $data['_id'];
        unset($data['_id']);
        $hasCaptcha = Db::connect('mongodb')->name('captcha_list')->where('_id', $_id)->find();
        if (empty($hasCaptcha)) {
          return Result::Error('配置不存在或已删除');
        }
        $status = Db::connect('mongodb')->name('captcha_list')->where('_id', $_id)->update($data);
        $tag = '编辑';
      } else {
        unset($data['id']);
        $data['create_time'] = time();
        $data['uid'] = $this->uid;
        $data['_id'] = createId();
        $status = Db::connect('mongodb')->name('captcha_list')->insert($data);
        $tag = '新增';
      }
      if ($status > 0) {
        return json(['code' => 200, 'data' => '', 'msg' => $tag . '成功']);
      } else {
        return json(['code' => -1, 'data' => '', 'msg' => $tag . '失败，请重试']);
      }
    } else {
      $id = input('param._id', '', 'trim');
      if (!$id) {
        return json(['code' => 200, 'data' => [], 'msg' => 'success']);
      }
      $data = Db::connect('mongodb')->name('captcha_list')->where('_id', $id)->find();
      return json(['code' => 200, 'data' => $data, 'msg' => 'success']);
    }
  }
  /**
   * 删除文章
   * @Author Qianlong <87498106@qq.com>
   * @PersonSite http://www.21ds.cn/
   */
  public function delItem()
  {
    $rule = [
      'id'  => 'require',
    ];
    $data = [
      'id' => input('param.id', '', 'trim'),
    ];
    $validate = Validate::rule($rule);
    $result   = $validate->check($data);
    if (!$result) {
      return Result::Error($validate->getError());
    }
    $status = Db::connect('mongodb')->name('captcha_list')->whereIn('_id', $data['id'])->delete();
    Db::connect('mongodb')->name('captcha_log')->whereIn('c_id', $data['id'])->delete();
    if ($status > 0) {
      return json(['code' => 200, 'data' => '', 'msg' => '删除成功']);
    } else {
      return json(['code' => -1, 'data' => '', 'msg' => '删除失败，请稍后重试']);
    }
  }
  public function changeStatusItem()
  {
    $rule = [
      'id'  => 'require',
      'status'  => 'require',
    ];
    $data = [
      'id' => input('param.id', '', 'trim'),
      'status' => input('param.status', '', 'trim'),
    ];
    $validate = Validate::rule($rule);
    $result   = $validate->check($data);
    if (!$result) {
      return Result::Error($validate->getError());
    }
    $status = Db::connect('mongodb')->name('captcha_list')->whereIn('_id', $data['id'])->update(['status'=>intval($data['status'])]);
    $msg = $data['status'] == 1 ? '启用' : '不启用';
    if ($status > 0) {
      return json(['code' => 200, 'data' => '', 'msg' => $msg.'成功']);
    } else {
      return json(['code' => -1, 'data' => '', 'msg' => $msg.'失败，请稍后重试']);
    }
  }
  public function del_item_batch()
  {
    $rule = [
      'id'  => 'require',
    ];
    $data = [
      'id' => input('param.ids/a', '', 'trim'),
    ];
    $validate = Validate::rule($rule);
    $result   = $validate->check($data);
    if (!$result) {
      return json(['code' => -1, 'data' => '', 'msg' => $validate->getError()]);
    }
    $status = Db::connect('mongodb')->name('captcha_list')->whereIn('_id', $data['id'])->delete();
    Db::connect('mongodb')->name('captcha_log')->whereIn('c_id', $data['id'])->delete();
    if ($status > 0) {
      return json(['code' => 200, 'data' => '', 'msg' => '删除成功']);
    } else {
      return json(['code' => -1, 'data' => '', 'msg' => '删除失败，请稍后重试']);
    }
  }
}
